package test.hxy.mongoSchoolSimple.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import test.hxy.mongoSchoolSimple.dao.CampusDao;
import test.hxy.mongoSchoolSimple.dao.CampusTenplateDao;
import test.hxy.mongoSchoolSimple.model.Campus;

import java.util.List;

@Service
public class CampusService {
    @Autowired
    CampusDao campusDao;
    @Autowired
    CampusTenplateDao campusTenplateDao;
    public List<Campus> findAll(){
        return campusDao.findAll();
    }

    public List<Campus> findByCampusNameContains(String campusName) {
        return campusDao.findByCampusNameContains(campusName);
    }

    public Campus findByCampusIdEquals(String campusId) {
        return campusDao.findByCampusIdEquals(campusId);
    }

    public Campus insertCampus(Campus campus) {
        campus.set_id(campus.getCampusId());
        Campus temp = campusDao.findByCampusIdEquals(campus.getCampusId());
        if (temp == null) {
            campusTenplateDao.insert(campus);
            return campus;
        } else {
            return null;
        }
    }

    public Campus delete(String campusId) {
        Campus temp = campusDao.findByCampusIdEquals(campusId);
        if (temp != null) {
            campusTenplateDao.delete(campusId);
            return temp;
        } else {
            return null;
        }
    }
}
